package googleJam.problem;

import java.util.LinkedList;
import java.util.List;

import googleJam.file.FileManager;
import googleJam.file.IFileManagerListener;

public class Problem implements IFileManagerListener {
	protected int _caseTestNumber;
	private LinkedList<CaseTest> _caseTests;
	private StringBuilder _stringBuilder;
	private CaseTest _curCaseTest;
	
	private static Problem s_sharedInstance;
	
	public Problem ()
	{
		_stringBuilder = new StringBuilder();
	}
	
	@Override
	public String getInputNameFilePath() {
		return Setting.getInputFile();
	}

	@Override
	public void onReadingLine(String line , int index) {
		
		if(index == 0)
		{
			_caseTestNumber = Integer.valueOf(line);
			_caseTests = new LinkedList<CaseTest>();
		}
		else if (_curCaseTest == null)
		{
			_curCaseTest = Setting.createCaseTest(line, 1,1);
			_caseTests.add(_curCaseTest);
		}
		else if (index == (_curCaseTest.getEndIndex() + 1))
		{
			_curCaseTest = Setting.createCaseTest(line, index,_curCaseTest.getEndIndex() + 1);
			_caseTests.add(_curCaseTest);
		}
		
		if(_curCaseTest != null)
		{
			_curCaseTest.trait(line, index - _curCaseTest.getBeginIndex());
		}
	}
	
	@Override
	public void onFinishingExecute() {
		for(CaseTest caseTest : _caseTests)
		{
			caseTest.printResult();
		}
		FileManager.getSharedManager().writeFile(_stringBuilder.toString(), "output.out");
	}
	
	public List<CaseTest> getCaseTests () { return _caseTests; }
	
	public StringBuilder getStringBuilder() { return _stringBuilder; }
	
	public static Problem getSharedInstance() 
	{
		if (s_sharedInstance == null)
		{
			s_sharedInstance = new Problem();
		}
		return s_sharedInstance;
	}
	
	
}
